<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">
                <div class="table-search-container">
                    <el-form ref="tableSearchRef" :model="state.ruleForm" size="default" label-width="100px" class="table-form">
                        <el-row>
                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
                                <el-form-item label="账户地址">
                                    <el-input v-model="state.ruleForm.address" style="width: 100%" placeholder="账户地址"></el-input>
                                </el-form-item>
                            </el-col>

                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="交易队">
                                    <el-select v-model="state.ruleForm.swapPairId" placeholder="请选择" clearable style="width: 100%">
                                        <el-option v-for="i in state.swapPairList" :key="i.swapPairId" :value="i.swapPairId"
                                            :label="i.coin1Name + '_' + i.coin2Name">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>



                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="支出币种">
                                    <el-select v-model="state.ruleForm.fromCoinId" placeholder="请选择" clearable style="width: 100%">

                                        <el-option v-for="i in state.coinList" :key="i.coinId" :value="i.coinId" :label="i.coinSymbol + '(' + i.coinName + ')'">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="收入币种">
                                    <el-select v-model="state.ruleForm.toCoinId" placeholder="请选择" clearable style="width: 100%">

                                        <el-option v-for="i in state.coinList" :key="i.coinId" :value="i.coinId" :label="i.coinSymbol + '(' + i.coinName + ')'">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>


                            <el-col :xs="24" :sm="12" :md="9" :lg="7" :xl="5" class="mb20" v-show="state.isToggle">
                                <el-form-item label="交易时间">
                                    <el-date-picker v-model="state.ruleForm.createDate" type="datetimerange" :picker-options="state.pickerOptions"
                                        range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right" style="width: 100%">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>

                            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20" v-show="state.isToggle">
                                <el-form-item label="系统地址">
                                    <el-select v-model="state.ruleForm.address" placeholder="系统地址" clearable style="width: 100%">
                                        <el-option v-for="i in state.systemAddressList" :key="i.address" :value="i.address"
                                            :label="i.addressTypeName + '[' + i.address + ']'">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>

                            <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                                <el-form-item class="table-form-btn" :label-width="!state.isToggle ? '10px' : '35px'">
                                    <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">
                                        <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
                                        <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
                                    </div>
                                    <div>
                                        <el-button size="default" type="primary" @click="refresh()">查询
                                        </el-button>
                                        <el-button size="default" type="info" class="ml10" @click="handleResetSearch()"> 重置
                                        </el-button>
                                    </div>
                                </el-form-item>
                            </el-col>

                        </el-row>
                    </el-form>
                </div>


                <el-table :default-sort="{ prop: 'CREATED_DATE', order: 'descending' }" size="default" :data="state.dataList" style="width: 100%" border=""
                    @sort-change="onSortHandler" :header-cell-class-name="handleHeaderClass">

                    <el-table-column align="center" label="id" width="80" :show-overflow-tooltip="true">
                        <template #default="scope">
                            <span>{{ scope.row.flashSwapRecordId }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="账户地址" :show-overflow-tooltip="true">
                        <template #default="scope">
                            <span>{{ scope.row.address }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="支出" width="220" :show-overflow-tooltip="true" sortable="custom" prop="from_Coin_Amount">
                        <template #default="scope">
                            <span>{{ scope.row.fromCoinAmount }}{{ ' ' + scope.row.fromCoinSymbol }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="手续费" width="100">
                        <template #default="scope">
                            <span>{{ scope.row.charge }}{{ scope.row.fromCoinSymbol }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="收入" width="220" :show-overflow-tooltip="true" sortable="custom" prop="to_Coin_Amount">
                        <template #default="scope">
                            <span>{{ scope.row.toCoinAmount }}{{ ' ' + scope.row.toCoinSymbol }}</span>
                        </template>
                    </el-table-column>



                    <el-table-column align="center" label="成交价" width="220">
                        <template #default="scope">
                            <span>{{ scope.row.price }} {{ ' '+ scope.row.priceTag}} </span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="交易时间" sortable="custom" prop="CREATED_DATE" width="180">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createdDate) }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="更新时间" width="180" prop="update_Date">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.updatedDate) }}</span>
                        </template>
                    </el-table-column>



                </el-table>
            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15" :pager-count="5" :page-sizes="[20, 50, 100]"
                v-model:current-page="state.page" background v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>
        </el-card>

    </div>
</template>

<script setup lang="ts" name="systemMenu">
import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

import { useFundApi } from '/@/api/fund/index'

import { flashSwapApi } from '/@/api/flashSwap/index';

const state = reactive({
    sorts: {} as any,
    isToggle: true,
    page: 1,
    limit: 20,
    total: 0,
    typeList: [] as any,
    dataList: [] as any,
    coinList: [] as any,
    swapPairList: [] as any,
    swapPairMap: {},
    systemTypeSelect: [] as any,// 系统账户类型
    ruleForm: {
        fromCoinId: "",
        toCoinId: "",
        swapPairId: "",
    },
    pickerOptions: {
        shortcuts: [
            {
                text: "最近一周",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 7
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近一个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 30
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
            {
                text: "最近三个月",
                onClick(picker: any) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(
                        start.getTime() - 3600 * 1000 * 24 * 90
                    );
                    picker.$emit("pick", [start, end]);
                },
            },
        ],
    },
    systemAddressList: [] as any

});


const refresh = () => {
    queryFlashSwapdRecord();
}



const handleResetSearch = () => {
    state.ruleForm = {
    };
    state.page = 1;
    state.limit = 20;
    queryFlashSwapdRecord();
}


const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM:SS")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    queryFlashSwapdRecord();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    queryFlashSwapdRecord();
};


const queryFlashSwapdRecord = async () => {
    let vo = {
        page: state.page,
        limit: state.limit
    } as any;

    let startTime, endTime;
    if (state.ruleForm.createDate && state.ruleForm.createDate.length > 0) {
        startTime = state.ruleForm.createDate[0].getTime();
        endTime = state.ruleForm.createDate[1].getTime();
        vo["createdDate"] = [startTime, endTime];
    }

    if (sortsStr()) vo["sorts"] = sortsStr();

    let listTemp = await flashSwapApi().swapRecordList(state.ruleForm, vo);
    state.dataList = listTemp.list;
    state.total = listTemp.total;
}


const querySystemAddress = async () => {
    let listTemp = await useFundApi().querySystemAddress({ page: 1, limit: 200 });
    state.systemAddressList = listTemp.list;
}


const getCoinList = async () => {
    let listTemp = await useFundApi().getcoinList();
    state.coinList = listTemp;
}

const getSwapPairList = async () => {
    let listTemp = await flashSwapApi().swapPairList({}, { limit: 100000 });
    state.swapPairList = listTemp.list;
}


const sortsStr = () => {
    let result = "";
    for (var prop in state.sorts) {
        result += prop + (state.sorts[prop] == "descending" ? " desc," : " asc,")
    }
    if (result == "") {
        return null;
    }
    return result.substring(0, result.length - 1)
}

const onSortHandler = (args: any) => {
    if (args.order) {
        state.sorts[args.prop] = args.order;
    } else {
        delete state.sorts[args.prop];
    }
    queryFlashSwapdRecord();
}

const handleHeaderClass = (column: any) => {
    if (column.column.property) {
        column.column.order = state.sorts[column.column.property];
    }
};



// 页面加载时
onMounted(() => {
    getCoinList();
    getSwapPairList();
    querySystemAddress();
    onSortHandler({ prop: "CREATED_DATE", order: "descending" });
    // flashSwapdAp();
});


</script>


<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}


.table-search-container {
    display: flex;

    .table-form {
        flex: 1;

        .table-form-btn-toggle {
            white-space: nowrap;
            user-select: none;
            display: flex;
            align-items: center;
            color: var(--el-color-primary);
            margin-right: 15px;
        }
    }
}
</style>

